#ifdef __APPLE__
        #include <OpenGL/gl.h>
        #include <OpenGL/glu.h>
        #include <GLUT/glut.h>
#else
	#ifdef _WIN32
        #include "../glut.h"
	#else
        #include <GL/gl.h>
        #include <GL/glu.h>
        #include <GL/glut.h>
	#endif
#endif

#include <iostream>

#include "../inc/Segment.h"

using namespace std;

Segment::Segment()
{
	p1.setXY(0,0);
	p2.setXY(0,0);
}

Segment::Segment(int _x, int _y, int _a, int _b)
{
	p1.setXY(_x, _y);
	p2.setXY(_a, _b);
}

Segment::Segment(Point &_p1, Point &_p2)
{
	p1.setXY( _p1.getX(), _p1.getY() );
	p2.setXY( _p2.getX(), _p2.getY() );
}

Segment::~Segment()
{
	
}

Point& Segment::getP1()
{
	return p1;
}

Point& Segment::getP2()
{
	return p2;
}

void Segment::setP1(Point _p)
{
	p1.setXY( _p.getX(), _p.getY() );
}

void Segment::setP2(Point _p)
{
	p2.setXY( _p.getX(), _p.getY() );
}

Segment Segment::operator =( const Segment &_s )
{
	Segment rslt;
	
	rslt.p1 = _s.p1;
	rslt.p2 = _s.p2;
	
	return rslt;
}

bool Segment::intersect(Segment &_s)
{	
	float x_ac, x_ad, x_bc, x_bd, x_ca, x_cb, x_da , x_db;
	float y_ac, y_ad, y_bc, y_bd, y_ca, y_cb, y_da , y_db; 
	float signe_acad, signe_bcbd, signe_cacb, signe_dadb;
	
	x_ac = _s.getP1().getX() - p1.getX();
	x_ad = _s.getP2().getX() - p1.getX();
	x_bc = _s.getP1().getX() - p2.getX();
	x_bd = _s.getP2().getX() - p2.getX();
	
	x_ca = - x_ac;
	x_cb = - x_bc;
	x_da = - x_ad;
	x_db = - x_bd;

	y_ac = _s.getP1().getY() - p1.getY();
	y_ad = _s.getP2().getY() - p1.getY();
	y_bc = _s.getP1().getY() - p2.getY();
	y_bd = _s.getP2().getY() - p2.getY();
	
	y_ca = - y_ac;
	y_cb = - y_bc;
	y_da = - y_ad;
	y_db = - y_bd;
	
	signe_acad = ( x_ac * y_ad ) - ( x_ad * y_ac );
	signe_bcbd = ( x_bc * y_bd ) - ( x_bd * y_bc );
	signe_cacb = ( x_ca * y_cb ) - ( x_cb * y_ca );
	signe_dadb = ( x_da * y_db ) - ( x_db * y_da );
	
	if ( signe_acad * signe_bcbd < 0 )
	/* les deux sont de signes differents */
	{
		if ( signe_cacb * signe_dadb < 0 )
		/* signes differents */
		{
			return true;
		}
		
		else
		{
			return false;
		}
	}
	
	else
	{
		return false;
	}
}

void Segment::print()
{
	cout<<"Segment :"<<endl;
	p1.print();
	p2.print();
}

void Segment::display()
{
	glBegin(GL_LINES);
		glVertex2i( p1.getX(), p1.getY() );
		glVertex2i( p2.getX(), p2.getY() );
	glEnd();
}

void Segment::display(float _r, float _g, float _b)
{
	glColor3f(_r,_g,_b);
	glBegin(GL_LINES);
		glVertex2i( p1.getX(), p1.getY() );
		glVertex2i( p2.getX(), p2.getY() );
	glEnd();
}
